package com.educhains.web.interceptor;

import com.educhains.module.user.vo.User;
import com.educhains.web.util.JsonUtils;
import com.educhains.web.util.SessionUtils;
import com.educhains.web.util.WebUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author YuRun
 * @since 2017年8月20日
 */
public class AccessControlInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
        User sessionUser = SessionUtils.getUser(request);
        if (sessionUser != null && (sessionUser.getUserAuthority() != User.USER_AUTHORITY_APPLYING)
                && (sessionUser.getUserAuthority() != User.USER_AUTHORITY_FROZEN)) {
            return true;
        } else {
            response.sendRedirect(WebUtils.getRequestPath(request, "/loginPage", false, false));
            //request.getRequestDispatcher("frontend/index/loginPage").forward(request,response);
            return false;
        }
    }

    public void postHandle(HttpServletRequest arg0, HttpServletResponse response, Object object, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception)
            throws Exception {
    }
}
